package package1.week1;

/**
 * @ClassName : 线性查找
 * @Author : 骆发茂
 * @Date: 2021/10/13 22:55
 * @Description :
 */
/*一一个个查找，线性查找法*/
public class 线性查找 {


    /*线性查找方法,标志上static，可以不用创建对象了*/
    public static int search(int[] data, int target) {
        for (int i = 0; i < data.length; i++) {
            if (data[i] == target)
                return i;
        }
        return -1;
    }



    /*外面就不可创建方法的构造对象了，但是里面可以*/
    private 线性查找() {

        /*泛型之后*/
        Integer[] data1 = {3, 14, 23, 423, 5, 34534, 52};
        int target = 14;
        int search3 = 定义泛型之后(data1, target);

    }


    /*线性查找方法,修改*/
    public static <E> int 定义泛型之后(E[] data, E target) {
        for (int i = 0; i < data.length; i++) {
            /*Integer，要改成equals（），进行值比较，不然使用==，会是引用比较了*/
            if (data[i].equals(target))
                return i;
        }
        return -1;
    }

    /*简单测试*/
    public static void main(String[] args) {
        /*简单初始情况*/
        int[] data = {1, 2, 3, 4, 5, 0, 6, 7, 7, 8, 9};
        int target = 1;
        线性查找 linearSearch = new 线性查找();
        int search = linearSearch.search(data, target);
        int search1 = linearSearch.search(data, 5);
        System.out.println(search);
        System.out.println(search1);

        /*static之后，不用创建方法的对象，可以直接调用方法名*/
        int search2 = 线性查找.search(data, 5);
        System.out.println(search2);


        /*Student比较*/
        Student[] students = {new Student("Alice"),
                new Student("Bobo"),
                new Student("Charles")};

        Student bobo = new Student("bobo");
        int i = 线性查找.定义泛型之后(students, bobo);
        System.out.println(i);

    }



}
